package com.notary.controller;


import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.notary.entity.CustomLog;
import com.notary.entity.Notary;
import com.notary.entity.vo.JsonResult;
import com.notary.mapper.CustomMapper;
import com.notary.service.CustomLogService;
import com.notary.util.DesUtil;
import com.notary.util.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import  com.notary.service.CustomService;
import com.notary.entity.Custom;

import java.util.List;
import java.util.UUID;

/**
* <p>
* 客户合作表 前端控制器
* </p>
* @author xujun
* @since 2020-09-11
*/


@Api(tags = "客户合作表控制器")
@RequestMapping("/custom")
@RestController
@Slf4j
public class CustomController{


    CustomService customService;


    @Autowired
    CustomMapper customMapper;

    @Autowired
    CustomLogService logService;

    @Autowired
    public  CustomController(CustomService customService){
        this.customService = customService;
    }


    @Value("${office.deskey}")
    private String deskey;
    /**
     * <p>
     * 根据条件查询全部记录（并翻页）
     * </p>
     */

    @ApiOperation(value = "一级列表查询")
    @PostMapping ("/list")
    public JsonResult<IPage<JSONObject>> list(@RequestBody Custom custom, int pageNum, int pageSize) {
        IPage<JSONObject> iPage = customMapper.getCustomList(new Page<>(pageNum,pageSize),custom);
        return JsonResult.success(iPage);
    }

    /**
     *<P>
     * 根据ID查找返回单个对象
     *</P>
     */
    @ApiOperation(value = "根据当前id，获取下级机构")
    @GetMapping("/find/{id}")
    public JsonResult<Custom> getById(@PathVariable("id") Integer id) {
        Custom custom=customService.getById(id);
        QueryWrapper<Custom> queryWrapper= new QueryWrapper<Custom>();
        queryWrapper.eq("pid",custom.getCustomId());
        List<Custom> list=customService.list(queryWrapper);
        return JsonResult.success(list);
    }

    @ApiOperation(value = "根据当前二级机构详情")
    @GetMapping("/findTwo/{id}")
    public JsonResult<Custom> findTwo(@PathVariable("id") Integer id) {
        Custom custom=customService.getById(id);
        QueryWrapper<CustomLog> queryWrapper= new QueryWrapper<CustomLog>();
        queryWrapper.eq("custom_id",custom.getCustomId());
        custom.setCustomLogList(logService.list(queryWrapper));
        return JsonResult.success(custom);
    }

    @ApiOperation(value = "保存二级公证处下的机构")
    @PostMapping("/saveTwoLever")
    public JsonResult<Custom> saveTwoLever(@RequestBody Custom custom) throws  Exception{
       if(custom.getPid()==null){
           return JsonResult.fail("pid不能为空");
       }
       if(custom.getId()==null){
           custom.setCustomId(UUID.randomUUID().toString().replaceAll("-", ""));
       }
        return customService.saveOneLever(custom);
    }



}
